/*实用类的设计*/
// 获取系统时间
// • 判断⽂件是否存在
// • 获取⽂件的所在⽬录路径
// • 创建⽬录
#pragma once
#include <iostream>
#include <string>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

namespace wu
{
    namespace util
    {
        class Data
        {
        public:
            static size_t getTime()
            {
                return (size_t)time(nullptr);
            }
        };

        class File
        {
        public:
            static bool exists(const std::string &pathname)
            {
                // access接口不具有跨平台性
                // return access(pathname.c_str(), F_OK) == 0;

                struct stat st;
                return stat(pathname.c_str(), &st) == 0;
            }
            static std::string path(const std::string &pathname)
            {
                //./a/b/c
                size_t pos = pathname.find_last_of("/\\");
                if(pos == std::string::npos)
                {
                    return ".";
                }
                return pathname.substr(0, pos+1);

            }
            static void createDirectory(const std::string &pathname)
            {
                size_t pos = 0, idex = 0;
                while(idex < pathname.size())
                {
                    pos = pathname.find_first_of("/\\", idex);
                    if(pos == std::string::npos)
                    {
                        mkdir(pathname.c_str(), 0777);
                    }

                    std::string parent_dir = pathname.substr(0, pos+1);//找父级目录
                    if(exists(parent_dir) == true)
                    {
                        idex = pos+1;
                        continue;
                    }
                    mkdir(parent_dir.c_str(), 0777);
                    idex = pos+1;
                }
            }
        };
    }
}
